package com.neu.servlet.klz;

import com.neu.service.klz.BeatchService;
import com.neu.util.ExcelUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

@WebServlet(name = "GoodBeatchServlet", urlPatterns = {"/GoodBeatchServlet.do"})
@MultipartConfig
public class GoodBeatchServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Part part=request.getPart("goods");
//        String n=request0.getParameter("u_name");
        String fileName=part.getHeader("Content-Disposition");
        String file=fileName.substring(fileName.lastIndexOf("."),fileName.length()-1);
        if (".xlsx".equals(file)) {
            InputStream in = part.getInputStream();
            List<Map<String, String>> list = ExcelUtil.readExcel(in);
            System.out.println(list);
            BeatchService beatchService = new BeatchService();
            boolean flag = beatchService.doSave(list);
            request.setAttribute("flag", flag);
            request.getRequestDispatcher("WEB-INF/views/mgr.jsp").forward(request, response);
//        if (flag){
//            String json=JSONObject.toJSONString(list);
//            System.out.println(json);
//            PrintWriter out=response.getWriter();
//            out.print(json);
//            out.close();
//        }else{
//
//        }
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
